#include <bits/stdc++.h>
#define int long long
using namespace std;

template<typename T>
T read(T x)
{
    T opt = 1, sum = 0;
    char ch = getchar();
    while(!isdigit(ch)) opt = (ch == '-') ? -1 : 1, ch = getchar();
    while( isdigit(ch)) sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar();
    return opt * sum;
}
#define read read(0ll)
inline void put(int x)
{
    if(x > 9) put(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e5 + 5;
int a[N];
int n;
int col[N];
int ans = INT_MIN;
void dfs(int x){
    if(x == n + 1){
        map<int, int> mp;
        int res = 0;
        for(int i = 1;i <= n;i ++ ){
            if(mp[a[i]]){
                if(col[mp[a[i]]] != col[i]) continue;
                int f = 1;
                for(int j = mp[a[i]] + 2;j < i;j ++ ){
                    if(col[j] != col[j - 1]) {
                        f = 0;
                        break;
                    }
                }
                if(f) res += a[i];
            }
            mp[a[i]] = i;
        }
        ans = max(ans, res);
        return ;
    }
    col[x] = 1;
    dfs(x + 1);
    col[x] = 0;
    dfs(x + 1);
}
void work()
{
    memset(col, 0, sizeof(col));
    n = read;
    ans = INT_MIN;
    for(int i = 1;i <= n;i ++ ){
        a[i] = read;
    }
    if(n <= 15){
    dfs(1);
    cout << ans << endl;}
    else {
		int res = 0;
		map<int, int> mp;
		for(int i = 1;i <= n;i ++ ){
			if(mp[a[i]]){
				if(i - mp[a[i]] + 1 < 5) res ++ ;
			}
			mp[a[i]] = i;
		}
		cout << res << endl;
	}
}
signed main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
    int T = read;
    while(T -- ) work();
    return 0;
}
